//封装本地数据缓存操作
module.exports = {
  set(key, value, sync = true){
    return new Promise((resolve, reject) => {
      if(sync) {
        try {
          wx.setStorageSync(key, value)
          resolve()
        } catch(e) {
          console.log(e)
          reject(e)
        }
      } else {
        wx.setStorage({
          key: key,
          data: value,
          success: () => {
            resolve()
          },
          fail: () => {
            reject()
          }
        })
      }
    })
  },
  get(key, sync = true){
    return new Promise((resolve, reject) => {
      if(sync) {
        try {
          let value = wx.getStorageSync(key)
          resolve(value)
        } catch(e) {
          reject(e)
        }
      } else {
        wx.getStorage({
          key: key,
          success: (res) => {
            console.log(res.data)
            resolve(res.data)
          },
          fail: r => {
            reject(r)
          }
        })
      }
    })
  },
  remove(key, sync = true){
    return new Promise((resolve, reject) => {
      if(sync) {
        try {
          wx.removeStorageSync(key)
          resolve()
        } catch(e) {
          console.log(e)
          reject(e)
        }
      } else {
        wx.removeStorage({
          key: key,
          success: () => {
            resolve()
          },
          fail: r => {
            reject(r)
          }
        })
      }
    })
  },
  clear(sync = false){
    return new Promise((resolve, reject) => {
      if(sync) {
        try {
          wx.clearStorageSync()
          resolve()
        } catch(e) {
          console.log(e)
          reject()
        }
      } else {
        wx.clearStorage()
      }
    })
  }
}
